package cn.tpshion.devops.node.support;

import cn.tpshion.devops.common.enums.MsgType;
import cn.tpshion.devops.domain.entity.MsgData;
import cn.tpshion.devops.handle.SSEHandle;
import com.jcraft.jsch.SftpProgressMonitor;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
public class MsgSftpProgressMonitor implements SftpProgressMonitor {

    private SSEHandle sseHandle;

    private NodeContext nodeContext;

    private String src;

    private AtomicLong atomicLong = new AtomicLong();

    private long max = 0;

    public MsgSftpProgressMonitor(SSEHandle sseHandle, NodeContext nodeContext){
        this.sseHandle = sseHandle;
        this.nodeContext = nodeContext;
    }

    @Override
    public void init(int op, String src, String dest, long max) {
        log.info("upload start op:{} src:{} dest:{} max:{}", op, src, dest, max);
        this.src = src;
        this.max = max;
        sseHandle.sendMessage(nodeContext, new MsgData(MsgType.INFO, "upload start src:" + src + " dest:" + dest));
    }

    @Override
    public boolean count(long count) {
        //log.info("fileName:{} upload: {}/{}", src, DataSizeUtil.format(atomicLong.addAndGet(count)), DataSizeUtil.format(max));
        //sseHandle.sendMessage(taskId, new MsgData(MsgType.INFO, "fileName:" + src + " upload: "+ DataSizeUtil.format(atomicLong.addAndGet(count))));
        return true;
    }

    @Override
    public void end() {
        log.info("upload success");
    }
}
